package com.lineage.william;

import com.lineage.DatabaseFactory;
import com.lineage.server.model.Instance.L1ItemInstance;
import com.lineage.server.model.Instance.L1PcInstance;
import com.lineage.server.model.Instance.L1SkinInstance;
import com.lineage.server.serverpackets.S_HPUpdate;
import com.lineage.server.serverpackets.S_MPUpdate;
import com.lineage.server.serverpackets.S_SPMR;
import com.lineage.server.serverpackets.S_ServerMessage;
import com.lineage.server.serverpackets.S_SystemMessage;
import com.lineage.server.serverpackets.ServerBasePacket;
import com.lineage.server.utils.L1SpawnUtil;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.StringTokenizer;

public class ItemOutburst {
  private static ArrayList<ArrayList<Object>> aData = new ArrayList<>();
  
  public static final HashMap<Integer, ItemOutburst> _questionIdIndex = new HashMap<>();
  
  private static boolean NO_GET_DATA = false;
  
  public static final String TOKEN = ",";
  
  private static ItemOutburst _instance;
  
  public static ItemOutburst getInstance() {
    if (_instance == null)
      _instance = new ItemOutburst(); 
    return _instance;
  }
  
  public static void reload() {
    _questionIdIndex.clear();
    _instance = null;
    getInstance();
  }
  
  private ItemOutburst() {
    getData();
  }
  
  public static void trueOutburst(L1PcInstance pc, L1ItemInstance item) {
    ArrayList<Integer> aTempData = null;
    if (!NO_GET_DATA) {
      NO_GET_DATA = true;
      getData();
    } 
    int itemid = item.getItemId();
    L1ItemInstance weapon = pc.getWeapon();
    int i = 0;
    while (i < aData.size()) {
      aTempData = (ArrayList)aData.get(i);
      if (((Integer)aTempData.get(0)).intValue() == itemid && (
        (Integer)aTempData.get(0)).intValue() == itemid && 
        pc.getLevel() >= ((Integer)aTempData.get(1)).intValue() && 
        pc.getLevel() <= ((Integer)aTempData.get(2)).intValue() && !pc.getOutbur()) {
        pc.setOutbur(true);
        if (((Integer)aTempData.get(3)).intValue() != 0) {
          pc.addDmgup(((Integer)aTempData.get(3)).intValue());
          pc.sendPackets((ServerBasePacket)new S_ServerMessage("近距离攻击+" + ((Integer)aTempData.get(3)).intValue()));
        } 
        if (((Integer)aTempData.get(4)).intValue() != 0) {
          pc.addBowDmgup(((Integer)aTempData.get(4)).intValue());
          pc.sendPackets((ServerBasePacket)new S_ServerMessage("远距离攻击+" + ((Integer)aTempData.get(4)).intValue()));
        } 
        if (((Integer)aTempData.get(5)).intValue() != 0) {
          pc.addClan_ReductionDmg(((Integer)aTempData.get(5)).intValue());
          pc.sendPackets((ServerBasePacket)new S_ServerMessage("减免伤害+" + ((Integer)aTempData.get(5)).intValue()));
        } 
        if (((Integer)aTempData.get(6)).intValue() != 0) {
          pc.addMaxHp(((Integer)aTempData.get(6)).intValue());
          pc.setCurrentHp(pc.getCurrentHp() + ((Integer)aTempData.get(6)).intValue());
          pc.sendPackets((ServerBasePacket)new S_ServerMessage("血量增加+" + ((Integer)aTempData.get(6)).intValue()));
        } 
        if (((Integer)aTempData.get(7)).intValue() != 0) {
          pc.addMaxMp(((Integer)aTempData.get(7)).intValue());
          pc.setCurrentMp(pc.getCurrentMp() + ((Integer)aTempData.get(7)).intValue());
          pc.sendPackets((ServerBasePacket)new S_ServerMessage("魔量增加+" + ((Integer)aTempData.get(7)).intValue()));
        } 
        if (((Integer)aTempData.get(8)).intValue() != 0) {
          pc.addSp(((Integer)aTempData.get(8)).intValue());
          pc.sendPackets((ServerBasePacket)new S_ServerMessage("魔攻增加+" + ((Integer)aTempData.get(8)).intValue()));
        } 
        if (((Integer)aTempData.get(9)).intValue() != 0) {
          L1SkinInstance skin = L1SpawnUtil.spawnSkin(pc, ((Integer)aTempData.get(9)).intValue());
          if (skin != null) {
            skin.setMoveType(1);
            pc.addSkin(skin, ((Integer)aTempData.get(9)).intValue());
          } 
        } 
        pc.sendPackets((ServerBasePacket)new S_HPUpdate(pc.getCurrentHp(), pc.getMaxHp()));
        pc.sendPackets((ServerBasePacket)new S_MPUpdate(pc.getCurrentMp(), pc.getMaxMp()));
        pc.sendPackets((ServerBasePacket)new S_SPMR(pc));
        if (""+aTempData.get(11) != null)
          pc.sendPackets((ServerBasePacket)new S_SystemMessage(""+aTempData.get(11))); 
        if (((Integer)aTempData.get(10)).intValue() == 1)
          pc.setcheckOutbur(true); 
        return;
      } 
      i++;
    } 
  }
  
  public static void falseOutburst(L1PcInstance pc, L1ItemInstance item) {
    ArrayList<Integer> aTempData = null;
    if (!NO_GET_DATA) {
      NO_GET_DATA = true;
      getData();
    } 
    int itemid = item.getItemId();
    int i = 0;
    while (i < aData.size()) {
      aTempData = (ArrayList)aData.get(i);
      if (((Integer)aTempData.get(0)).intValue() == itemid && (
        (Integer)aTempData.get(0)).intValue() == itemid && 
        pc.getLevel() >= ((Integer)aTempData.get(1)).intValue() && 
        pc.getLevel() <= ((Integer)aTempData.get(2)).intValue() && pc.getOutbur()) {
        pc.setOutbur(false);
        if (((Integer)aTempData.get(3)).intValue() != 0) {
          pc.addDmgup(-((Integer)aTempData.get(3)).intValue());
          pc.sendPackets((ServerBasePacket)new S_ServerMessage("近距离攻击-" + ((Integer)aTempData.get(3)).intValue()));
        } 
        if (((Integer)aTempData.get(4)).intValue() != 0) {
          pc.addBowDmgup(-((Integer)aTempData.get(4)).intValue());
          pc.sendPackets((ServerBasePacket)new S_ServerMessage("远距离攻击-" + ((Integer)aTempData.get(4)).intValue()));
        } 
        if (((Integer)aTempData.get(5)).intValue() != 0) {
          pc.addClan_ReductionDmg(-((Integer)aTempData.get(5)).intValue());
          pc.sendPackets((ServerBasePacket)new S_ServerMessage("减免伤害-" + ((Integer)aTempData.get(5)).intValue()));
        } 
        if (((Integer)aTempData.get(6)).intValue() != 0) {
          pc.addMaxHp(-((Integer)aTempData.get(6)).intValue());
          pc.setCurrentHp(pc.getCurrentHp() + ((Integer)aTempData.get(6)).intValue());
          pc.sendPackets((ServerBasePacket)new S_ServerMessage("血量增加-" + ((Integer)aTempData.get(6)).intValue()));
        } 
        if (((Integer)aTempData.get(7)).intValue() != 0) {
          pc.addMaxMp(-((Integer)aTempData.get(7)).intValue());
          pc.setCurrentMp(pc.getCurrentMp() + ((Integer)aTempData.get(7)).intValue());
          pc.sendPackets((ServerBasePacket)new S_ServerMessage("魔量增加-" + ((Integer)aTempData.get(7)).intValue()));
        } 
        if (((Integer)aTempData.get(8)).intValue() != 0) {
          pc.addSp(-((Integer)aTempData.get(8)).intValue());
          pc.sendPackets((ServerBasePacket)new S_ServerMessage("魔攻增加-" + ((Integer)aTempData.get(8)).intValue()));
        } 
        if (((Integer)aTempData.get(9)).intValue() != 0 && 
          pc.getSkin(((Integer)aTempData.get(9)).intValue()) != null) {
          pc.getSkin(((Integer)aTempData.get(9)).intValue()).deleteMe();
          pc.removeSkin(((Integer)aTempData.get(9)).intValue());
        } 
        pc.sendPackets((ServerBasePacket)new S_HPUpdate(pc.getCurrentHp(), pc.getMaxHp()));
        pc.sendPackets((ServerBasePacket)new S_MPUpdate(pc.getCurrentMp(), pc.getMaxMp()));
        pc.sendPackets((ServerBasePacket)new S_SPMR(pc));
        if (""+aTempData.get(12) != null)
          pc.sendPackets((ServerBasePacket)new S_SystemMessage(""+aTempData.get(12))); 
        if (((Integer)aTempData.get(10)).intValue() == 1)
          pc.setcheckOutbur(false); 
        return;
      } 
      i++;
    } 
  }
  
  private static void getData() {
    Connection con = null;
    try {
      con = DatabaseFactory.get().getConnection();
      Statement stat = con.createStatement();
      ResultSet rset = stat.executeQuery("SELECT * FROM w_道具爆气系统");
      ArrayList<Object> aReturn = null;
      if (rset != null)
        while (rset.next()) {
          aReturn = new ArrayList();
          aReturn.add(0, new Integer(rset.getInt("道具编号")));
          aReturn.add(1, new Integer(rset.getInt("最小等级")));
          aReturn.add(2, new Integer(rset.getInt("最高等级")));
          aReturn.add(3, new Integer(rset.getInt("近增伤")));
          aReturn.add(4, new Integer(rset.getInt("远增伤")));
          aReturn.add(5, new Integer(rset.getInt("减伤")));
          aReturn.add(6, new Integer(rset.getInt("增血")));
          aReturn.add(7, new Integer(rset.getInt("增魔")));
          aReturn.add(8, new Integer(rset.getInt("魔攻")));
          aReturn.add(9, new Integer(rset.getInt("特效")));
          aReturn.add(10, new Integer(rset.getInt("每秒扣一怒气值")));
          aReturn.add(11, rset.getString("启动显示"));
          aReturn.add(12, rset.getString("关闭显示"));
          aData.add(aReturn);
        }  
      if (con != null && !con.isClosed())
        con.close(); 
    } catch (Exception exception) {}
  }
  
  private static Object getArray(String s, String sToken, int iType) {
    StringTokenizer st = new StringTokenizer(s, sToken);
    int iSize = st.countTokens();
    String sTemp = null;
    if (iType == 1) {
      int[] iReturn = new int[iSize];
      int i = 0;
      while (i < iSize) {
        sTemp = st.nextToken();
        iReturn[i] = Integer.parseInt(sTemp);
        i++;
      } 
      return iReturn;
    } 
    if (iType == 2) {
      String[] sReturn = new String[iSize];
      int i = 0;
      while (i < iSize) {
        sTemp = st.nextToken();
        sReturn[i] = sTemp;
        i++;
      } 
      return sReturn;
    } 
    if (iType == 3) {
      String sReturn2 = null;
      int i = 0;
      while (i < iSize) {
        sTemp = sReturn2 = st.nextToken();
        i++;
      } 
      return sReturn2;
    } 
    return null;
  }
}
